<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
    <div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <header style="height: 100%">
                <div align="left">
                    <table style="width:100%">
                        <tr>
                            <td>
                                <form class="form-inline" onsubmit="return false;">
                                    <div class="form-group">
                                        用户名：
                                        <input id="username" type="text" class="form-control" placeholder="用户名">
                                        昵称：
                                        <input id="nickname" type="text" class="form-control" placeholder="昵称">
                                        状态：
                                        <select class="form-control input-sm" id="enabled">
                                            <option value="">全部</option>
                                            <option value="1">正常</option>
                                            <option value="0">禁用</option>
                                        </select>
                                        类型：
                                        <select class="form-control input-sm" id="type">
                                            <option value="">全部</option>
                                            <option value="BACKEND">后端管理用户</option>
                                            <option value="APP">前端app用户</option>
                                        </select>
                                        <button id="searchBt" class="layui-btn layui-btn-sm" permission="back:user:query"><i class="layui-icon">&#xe615;</i>搜索</button>
                                    </div>
                                </form>
                            </td>

                            <td align="right">
                                <button class="layui-btn layui-btn-sm" onclick="location.href='addUser.html'">
                                    <i class="layui-icon">&#xe608;</i> 添加
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            </header>

            <div>
                <div class="widget-body no-padding">
                    <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                        <thead>
                        <tr>
                        </tr>
                        <tr>
                            <th>username</th>
                            <th>昵称</th>
                            <th>手机号</th>
                            <th>类型</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>
</body>

<script src="../../js/constant.js"></script>
<script type="text/javascript" src="../../js/libs/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>

<script type="text/javascript">
    var pers = checkPermission();
    var example;

    layui.use(['layer','util'],function(){
        var layer = layui.layer;
        var util = layui.util;
        init();
         function init(){
            example =
                $('#dt-table').DataTable({
                    "searching": false,
                    "processing": false,
                    "serverSide" : true,
                    "ordering" :false,
                    "language": {
                        "url": "../../js/plugin/datatables/Chinese.lang"
                    },
                    "ajax": {
                        "url" : domainName + "/api-u/users",
                        "type":"get",
                        "data":function(d){
                            d.username = $('#username').val();
                            d.nickname = $('#nickname').val();
                            d.enabled = $("#enabled").val();
                            d.type = $("#type").val();
                        },
                        "dataSrc": function (json) {
                            json.recordsTotal=json.total;
                            json.recordsFiltered=json.total;
                            return json.data;
                        },
                        "error":function(xhr, textStatus, errorThrown){
                            var msg = xhr.responseText;
                            console.log(msg);
                            if (xhr.status == 400) {
                                layer.msg(JSON.parse(msg).message);
                            } else if (xhr.status == 401) {
                                localStorage.removeItem("token");
                                layer.msg("token过期，请重新登录", {shift: -1, time: 1000}, function(){
                                    location.href = loginPage;
                                });
                            } else if (xhr.status == 403) {
                                layer.msg('未授权');
                            } else if (xhr.status == 500) {
                                var info = JSON.parse(msg);
                                var exception = info.exception;
                                var message = info.message;
                                layer.msg('系统错误：' + (exception ? exception : message));
                            }
                        }
                    },
                    "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",
                    "columns": [
                        { "data": "username", "defaultContent": ""},
                        { "data": "nickname", "defaultContent": ""},
                        { "data": "phone", "defaultContent": ""},
                        {
                            "data": "type",
                            "defaultContent": "",
                            "render": function (data, type, row) {
                                if(data =="BACKEND"){
                                    return "后端管理用户";
                                }
                                return "前端app用户";
                            }
                        },
                        {
                            "data": "enabled",
                            "defaultContent": "",
                            "render": function (data, type, row) {
                                if(data){
                                    return "正常";
                                }
                                return "禁用";
                            }
                        },
                        {
                            "data": "",
                            "defaultContent": "",
                            "render": function (data, type, row) {
                                var id = row['id'];
                                var href = "updateUser.html?id=" + id;
                                var edit = buttonEdit(href, "back:user:update", pers);

                                //重置密码
                                var resetPassword = "";
                                if($.inArray("back:user:password", pers) >= 0){
                                    var btn = $("<button class='layui-btn layui-btn-xs'><i class='layui-icon'>重置密码</i></button>");
                                    btn.attr("onclick", 'resetPassword(' +id+')');
                                    resetPassword = btn.prop("outerHTML");
                                }

                                //分配角色
                                var setRoles = "";
                                if($.inArray("back:user:role:set", pers) >= 0){
                                    var btn = $("<button class='layui-btn layui-btn-xs'><i class='layui-icon'>分配角色</i></button>");
                                    btn.attr("onclick", 'setRoles(' +id+')');
                                    setRoles = btn.prop("outerHTML");
                                }
                                return edit + resetPassword + setRoles;
                            }
                        },
                    ],
                });
        }
    });
</script>
</html>